
package com.fumihiko.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fumihiko.common.annotation.SysLog;
import com.fumihiko.common.utils.Constant;
import com.fumihiko.common.utils.PageUtils;
import com.fumihiko.common.utils.R;
import com.fumihiko.common.validator.ValidatorUtils;
import com.fumihiko.modules.sys.entity.SysDictEntity;
import com.fumihiko.modules.sys.entity.SysRoleEntity;
import com.fumihiko.modules.sys.service.SysDictService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @author devilzwy@gmail.com
 */
@RestController
@RequestMapping("sys/dict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 列表
     */
    @RequestMapping("/list/{type}")
    @RequiresPermissions("sys:dict:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("type") String type){
        String name = (String)params.get("name");
        QueryWrapper<SysDictEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(name),"value",name);
        queryWrapper.eq(StringUtils.isNotBlank(type),"type",type);
        queryWrapper.eq("del_flag",0);
        queryWrapper.orderByAsc("order_num");
        int pageNo = Integer.valueOf((String)params.get("page"));

        int pageSize = Integer.valueOf((String)params.get("limit"));

        Page<SysDictEntity> page = new Page<>(pageNo,pageSize);
        IPage<SysDictEntity> pageList =  sysDictService.selectDictDetailPage(page,queryWrapper);
        return R.ok().put("page", new PageUtils(pageList));
}

    /**
     * 列表首页
     */
    @RequestMapping("/dictList")
    @RequiresPermissions("sys:dict:dictList")
    public R dictList(@RequestParam Map<String, Object> params){

        String name = (String)params.get("name");
        QueryWrapper<SysDictEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(name),"name",name);
        int pageNo = Integer.valueOf((String)params.get("page"));

        int pageSize = Integer.valueOf((String)params.get("limit"));

        Page<SysDictEntity> page = new Page<>(pageNo,pageSize);
        IPage<SysDictEntity> pageList =  sysDictService.selectDictPage(page,queryWrapper);
        return R.ok().put("page", new PageUtils(pageList));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:dict:info")
    public R info(@PathVariable("id") Long id){
        SysDictEntity dict = sysDictService.getById(id);

        return R.ok().put("dict", dict);
    }

    /**
     * 字典列表
     */
    @GetMapping("/select/{type}")
    @RequiresPermissions("sys:dict:select")
    public R select(@PathVariable("type") String type){

        List<SysDictEntity> list = sysDictService.listByType(type);

        return R.ok().put("list", list);
    }


    /**
     * 字典列表
     */
    @GetMapping("/secondary/{val}")
    @RequiresPermissions("sys:dict:secondary")
    public R secondary(@PathVariable("val") String val){

        List<SysDictEntity> list = sysDictService.listByRemark(val);

        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @SysLog("保存数据字典")
    @RequestMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public R save(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.save(dict);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改数据字典")
    @RequestMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public R update(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.updateById(dict);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除数据字典")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public R delete(@RequestBody Long[] ids){
        sysDictService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
